import { DataSet } from 'choerodon-ui/pro';
import intl from 'utils/intl';
import { BASE_SERVER, API_HOST } from '@src/utils/constants';
import { getCurrentOrganizationId } from 'utils/utils';
import { DataSetSelection, FieldIgnore, FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import { AxiosRequestConfig } from 'axios';

const modelPrompt = 'tarzan.aps.materialsResources';

const tenantId = getCurrentOrganizationId();

// 表格dataSet
const listPageFactory = () =>
  new DataSet({
    primaryKey: 'exceptionResourceMaterialId',
    autoQuery: false,
    selection: DataSetSelection.multiple, 
// 接口返回数据源
    dataKey: 'rows.content', 
    // cacheSelection: true,
    totalKey: 'rows.totalElements',
// 表格查询条件dataSet
    queryDataSet: new DataSet({
      fields: [
        {
          name: 'scheduleSiteLov',
          label: intl.get(`${modelPrompt}.form.scheduleSite`).d('计划站点'),
          ignore: FieldIgnore.always,
          type: FieldType.object,
          lovCode: 'MT.APS.SITE.PERMISSION',
          lovPara: {
            tenantId,
          },
        },
        {
          name: 'scheduleSiteId',
          bind: 'scheduleSiteLov.siteId',
        },
        {
          name: 'materialLov',
          label: intl.get(`${modelPrompt}.form.material`).d('物料编码'),
          type: FieldType.object,
          ignore: FieldIgnore.always,
          lovCode: 'MT.APS.MATERIAL.SITE',
          dynamicProps: {
            lovPara: ({ record }) => {
              return {
                tenantId,
                scheduleSiteId: record.get('scheduleSiteId'),
              };
            },
          },
        },
        {
          name: 'materialId',
          bind: 'materialLov.materialId',
        },
        {
          name: 'resourceLov',
          label: intl.get(`${modelPrompt}.form.resource`).d('资源编码'),
          ignore: FieldIgnore.always,
          type: FieldType.object,
          lovCode: 'HPS.EXCEPTION_RESOURCE',
          dynamicProps: {
            lovPara: ({ record }) => {
              return {
                tenantId,
                scheduleSiteId: record.get('scheduleSiteId'),
              };
            },
          },
        },
        {
          name: 'exceptionResourceId',
          bind: 'resourceLov.exceptionResourceId',
        },
        {
          name: 'resourceTypeList',
          label: intl.get(`${modelPrompt}.form.resourceType`).d('资源类型'),
          multiple: true,
          type: FieldType.string,
          lookupCode: 'HPS.EXCEPTION_RESOURCE_TYPE',
          valueField: 'value',
        },
        {
          name: 'enableFlag',
          label: intl.get(`${modelPrompt}.form.enableFlag`).d('启用状态'),
          type: FieldType.string,
          lookupCode: 'MT.APS.YES_NO',
        },
      ],
    }),
    events: {
      update({ name, record }) {
        if (name === 'scheduleSiteCode') {
          if (!record.get('scheduleSiteCode') && record.get('materialCode')) {
            record.set('materialCode', undefined);
          }
          if (!record.get('scheduleSiteCode') && record.get('resourceCode')) {
            record.set('resourceCode', undefined);
          }
        }
      },
    },
// 表格列字段配置
    fields: [
      {
        name: 'orderNum',
        label: intl.get(`${modelPrompt}.table.orderNum`).d('序号'),
        type: FieldType.string,
      },
      {
        name: 'scheduleSiteCode',
        type: FieldType.object,
        label: intl.get(`${modelPrompt}.table.scheduleSiteCode`).d('站点'),
        lovCode: 'MT.APS.SITE.PERMISSION',
        //lov查询角色id，admin为0，普通用户为1
        lovPara: {
          tenantId,
        },
        required: true,
      },
      {
        name: 'scheduleSiteId',
        dynamicProps: {
          bind({ record }) {
            if (record.get('scheduleSiteCode') && record.get('scheduleSiteCode').siteId) {
              return 'scheduleSiteCode.siteId';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'materialCode',
        label: intl.get(`${modelPrompt}.table.materialCode`).d('物料编码'),
        required: true,
        lovCode: 'MT.APS.MATERIAL.SITE',
        type: FieldType.object,
        dynamicProps: {
          disabled({ record }) {
            if (record.get('scheduleSiteId')) {
              return false;
            } else {
              return true;
            }
          },
          lovPara({ record }) {
            return {
              tenantId,
              scheduleSiteId: record.get('scheduleSiteId'),
            };
          },
        },
      },
      {
        name: 'materialId',
        type: FieldType.string,
        dynamicProps: {
          bind({ record }) {
            if (record.get('materialCode') && record.get('materialCode').materialId) {
              return 'materialCode.materialId';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'materialName',
        type: FieldType.string,
        disabled: true,
        label: intl.get(`${modelPrompt}.table.materialName`).d('物料描述'),
        dynamicProps: {
          bind({ record }) {
            if (record.get('materialCode') && record.get('materialCode').materialId) {
              return 'materialCode.materialName';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'resourceCode',
        label: intl.get(`${modelPrompt}.table.resource`).d('资源编码'),
        lovCode: 'HPS.EXCEPTION_RESOURCE',
        required: true,
        ignore: FieldIgnore.always,
        type: FieldType.object,
        dynamicProps: {
          disabled({ record }) {
            if (record.get('scheduleSiteId')) {
              return false;
            } else {
              return true;
            }
          },
          lovPara: ({ record }) => {
            return {
              tenantId,
              scheduleSiteId: record.get('scheduleSiteId'),
            };
          },
        },
      },
      {
        name: 'exceptionResourceId',
        type: FieldType.string,
        dynamicProps: {
          bind({ record }) {
            if (record.get('resourceCode') && record.get('resourceCode').exceptionResourceId) {
              return 'resourceCode.exceptionResourceId';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'resourceDescribe',
        type: FieldType.string,
        disabled: true,
        label: intl.get(`${modelPrompt}.table.resourceDescribe`).d('资源描述'),
        dynamicProps: {
          bind({ record }) {
            if (record.get('resourceCode') && record.get('resourceCode').exceptionResourceId) {
              return 'resourceCode.resourceDescribe';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'resourceType',
        label: intl.get(`${modelPrompt}.table.resourceType`).d('资源类型'),
        disabled: true,
        type: FieldType.string,
        dynamicProps: {
          bind({ record }) {
            if (record.get('resourceCode') && record.get('resourceCode').exceptionResourceId) {
              return 'resourceCode.resourceType';
            } else {
              return undefined;
            }
          },
        },
        lookupCode: 'HPS.EXCEPTION_RESOURCE_TYPE',
        valueField: 'value',
      },
      {
        name: 'standardCapacityValue',
        label: intl.get(`${modelPrompt}.table.standardCapacityValue`).d('标准产能'),
        required: true,
        type: FieldType.number,
        min: 0,
      },
      {
        name: 'capacityUomCode',
        required: true,
        type: FieldType.object,
        label: intl.get(`${modelPrompt}.table.capacityUomCode`).d('产能单位'),
        lovCode: 'MT.APS.UOM',
        lovPara: {
          tenantId: getCurrentOrganizationId(),
          uomType: 'CAPACITY',
        },
      },
      {
        name: 'capacityUomId',
        dynamicProps: {
          bind({ record }) {
            if (record.get('capacityUomCode') && record.get('capacityUomCode').uomId) {
              return 'capacityUomCode.uomId';
            } else {
              return undefined;
            }
          },
        },
      },
      {
        name: 'enableFlag',
        label: intl.get(`${modelPrompt}.table.enableFlag`).d('启用状态'),
        required: true,
        lookupCode: 'MT.APS.YES_NO',
        type: FieldType.string,
        trueValue: 'Y',
        falseValue: 'N',
        defaultValue: 'Y',
      },
      {
        name: 'action',
        label: intl.get(`${modelPrompt}.table.action`).d('操作'),
      },
    ],
    transport: {
      read: (config: AxiosRequestConfig): AxiosRequestConfig => {
        return {
          ...config,
          url: `${API_HOST}${BASE_SERVER}/v1/${getCurrentOrganizationId()}/hps-exception-res-materials/list/for/ui`,
          transformResponse: (value) => {
            let listData: any = {};
            try {
              listData = JSON.parse(value);
            } catch (err) {
              listData = {
                message: err,
              };
            }
            if (!listData.success) {
              return {
                ...listData,
                failed: true,
              };
            } else {
              const { rows } = listData;
              const content = (rows.content || []).map((item, i) => {
                return {
                  ...item,
                  orderNum: i + 1,
                };
              });
              return {
                ...listData,
                rows: {
                  ...rows,
                  content,
                },
              };
            }
          },
        };
      },
    },
  });

export default listPageFactory;
